package src.Packets;

import src.Main;
import src.Players.*;
import src.World.*;

public class PickupItem
{
    public PickupItem(Player p)
    {
        if(p == null || p.inStream == null)
        {
            return;
        }
        if(!p.itemPickup)
        {
            p.clickY = p.inStream.readSignedWordBigEndian();
            p.clickId = p.inStream.readUnsignedWord();
            p.clickX = p.inStream.readSignedWordBigEndian();
        }
        int distance = Main.m.ge.getDistance(p.clickX, p.clickY, p.absX, p.absY);
        if(distance > 0 && (p.dir1 > 0 || p.dir2 > 0) || distance != 0 && p.dir1 <= 0 && p.dir2 <= 0)
        {
            p.itemPickup = true;
            return;
        }
        p.itemPickup = false;
        int idx = Main.m.ge.item.itemExists(p.clickId, p.clickX, p.clickY, p.heightLevel);
        if(idx == -1)
        {
            return;
        }
        GroundItem g = Main.m.ge.item.groundItems[idx];
        if(g != null)
        {
            PlayerItems pi = new PlayerItems();
            if(pi.addItem(p, g.getId(), g.getAmt()))
            {
                Main.m.ge.item.itemPickedup(g.getId(), g.getX(), g.getY(), p.heightLevel);
            }
            pi = null;
        }
    }
}